package com.cbgolf.oa.views;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseView;
import com.cbgolf.oa.presenter.IBillPresenter;
import com.cbgolf.oa.entity.BillBean;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.abslistview.CommonAdapter;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lq on 2017/11/2.
 */

public class BillHistoryView extends BaseView implements SwipeRefreshLayout.OnRefreshListener {
    @ViewInject(R.id.topBack_ll)
    private View topBack;
    @ViewInject(R.id.title_tv)
    private TextView titleTv;
    //
    @ViewInject(R.id.a_bill_history_swrlayout)
    private SwipeRefreshLayout refreshLayout;
    @ViewInject(R.id.a_bill_history_listview)
    private ListView listView;
    //    @ViewInject(R.id.g_error_ll)
//    private View errorView;
//    @ViewInject(R.id.g_error_loading_tv)
//    private TextView loadingTv;
//    @ViewInject(R.id.g_error_tv)
//    private TextView errorTv;
//    @ViewInject(R.id.g_error_reload_tv)
//    private TextView reloadTv;
    private ErrorView errorView;
    //
    private CommonAdapter<BillBean> adapter;

    @Override
    public void onRefresh() {
        worker.refresh();
    }

    private IBillPresenter worker;


    public BillHistoryView(Activity act, IBillPresenter worker) {
        this.worker = worker;
        ViewUtils.autoInjectAllField(this, act);
        super.init(act);
    }

    @Override
    protected void setViews() {
        titleTv.setText(TextUtil.Text(worker.getActivityTitle()));
        errorView = new ErrorView(context);
        adapter = new CommonAdapter<BillBean>(context, R.layout.item_billhistory, listData) {
            @Override
            public void convert(ViewHolder holder, final BillBean billBean) {
                holder.setText(R.id.item_billhistory_order_num_tv, "消费单号：" + TextUtil.Text(billBean.consumerOrderNo));
                holder.setText(R.id.item_billhistory_cardnum_tv, "消费卡号：" + TextUtil.Text(billBean.consumerCarNo));
                holder.setText(R.id.item_billhistory_username_tv, "姓名：" + TextUtil.Text(billBean.customerName));
                holder.setText(R.id.item_billhistory_time_tv, "开单时间：" + DateUtil.stampToDate(billBean.orderTime));
                holder.setText(R.id.item_billhistory_billname_tv, "开单人员：" + TextUtil.Text(billBean.orderPerson));
                holder.setText(R.id.item_billhistory_money_tv, "￥" + TextUtil.textNumFloat(billBean.payable));
                holder.setOnClickListener(R.id.item_billhistory_showdetails_tv, v -> worker.showDetails(billBean.consumerOrderId));
            }
        };
        listView.setAdapter(adapter);
        // 设置下拉圆圈上的颜色，蓝色、绿色、橙色、红色
        refreshLayout.setColorSchemeResources(R.color.bg_blue, R.color.text_gray,
                R.color.white, R.color.bg_blue);
//        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
//                android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        refreshLayout.setDistanceToTriggerSync(300);// 设置手指在屏幕下拉多少距离会触发下拉刷新
        refreshLayout.setSize(SwipeRefreshLayout.LARGE);
        refreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.white));
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void setListener() {
        topBack.setOnClickListener(v -> worker.back());


        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (listView.getAdapter() != null && listView.getLastVisiblePosition() == (listView.getAdapter().getCount() - 1)
                        && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    worker.loadMore();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

    }

    @Override
    public void showProgress() {
        if (!refreshLayout.isRefreshing()) {
            AnimaUtil.showLoading(context, errorView.getLoadingTv());
        }
    }

    @Override
    public void stopProgress() {
        if (null != refreshLayout) refreshLayout.setRefreshing(false);
        AnimaUtil.stopLoading();
    }

    @Override
    public void getDataFail(String msg) {
        if (Util.listIsNull(listData)) {
            errorView.showError(refreshLayout, msg, () -> worker.reLoad());
        }
    }

    @Override
    public void getDataFail(String msg, int code) {
        if (Util.listIsNull(listData))
            errorView.showError(refreshLayout, msg, code, () -> worker.reLoad());
    }

    public void showSuccess() {
        refreshLayout.setVisibility(View.VISIBLE);
    }

    List<BillBean> listData = new ArrayList<>();

    @Override
    public void getDataSuccess(Object obj) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        List<BillBean> list = (List<BillBean>) obj;
        if (Util.listIsNull(list)) {
            if (Util.listIsNull(listData))
                getDataFail(ErrorUtil.NODATA);
            return;
        }
        listData.clear();
        listData.addAll(list);
        showSuccess();
        adapter.notifyDataSetChanged();
    }

    public void showLoadMore(Object obj) {
        List<BillBean> list = null;
        try {
            list = (List<BillBean>) obj;
        } catch (Exception e) {

        }
        if (Util.listIsNull(list)) {
            return;
        }
        showSuccess();
        listData.addAll(list);
        adapter.notifyDataSetChanged();
    }

}
